package org.nf.trading.controller.app.order;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.controller.BaseController;
import org.nf.trading.entity.Order;
import org.nf.trading.service.app.order.OrderService;
import org.nf.trading.vo.ResultVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/order")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin
public class OrderController extends BaseController {
    private final OrderService orderService;

    @GetMapping("/seller/{sellerId}")
    public ResultVO listOrdersBySellerId(@PathVariable("sellerId") Integer sellerId){
        List<Order> orders = orderService.listOrdersBySeller(sellerId);
        return success(orders);
    }

    /**
     * 通过用户id查询订单列表
     * @param uid
     * @return
     */
    @GetMapping("/uid/{uid}")
    public ResultVO listOrderByUid(@PathVariable("uid") Integer uid){
        List<Order> orders = orderService.listOrdersByUid(uid);
        return success(orders);
    }

    /**
     * 通过订单id修改状态
     * @param orderId
     * @param status
     * @return
     */
    @GetMapping("/orderId/{orderId}/status/{status}")
    public ResultVO updateOrderStatus(@PathVariable("orderId") Integer orderId,@PathVariable("status") Integer status){
        boolean res = orderService.updateOrderStatus(orderId, status);
        return success(res);
    }

    /**
     * 通过订单id和用户id查询一个订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/orderId/{orderId}")
    public ResultVO getOrderIdAndUid(@PathVariable("orderId") Integer orderId){
        Order order = orderService.getOrderIdAndUid(orderId);
        return success(order);
    }

    /**
     * 通过用户和状态查询全部订单
     *
     * @param
     * @return
     */
    @GetMapping("/ordersUid/{buyerId}/status/{status}")
    public ResultVO getListOrderByStatus(
            @PathVariable("buyerId") Integer buyerId,
            @PathVariable("status") Integer status){
        List<Order> orders = orderService.listOrdersByStatue(buyerId,status);
        return success(orders);
    }

    /**
     * 添加订单
     *
     * 首先，用户在点击下单时候需要默认是未支付，设置状态为0，调用已完成的回调，转到另一个
     * 页面的是，发送请求，已支付状态。
     * @param order
     * @return
     */
    @PostMapping
    public ResultVO addOrder(@RequestBody Order order) {
        int added = orderService.addOrder(order);
        return success(200,"添加成功",added);
    }
}
